package com.crealth.handler;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crealth.db.EventRemindDb;
import com.crealth.model.EventRemindModel;
import com.crealth.msg.NetMessageData;


public class EventRemindHandler {

	public static NetMessageData addEventRemind(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");		
		EventRemindModel addRemind = new EventRemindModel();
		addRemind.parseFromJson(msgJson);
		addRemind.setUserID(userID);
		EventRemindDb.insertEventRemind(addRemind);
		if (addRemind.getId() <= 0) {
			return null;
		}
		
		resJson.put("resCode", 0);
		resJson.put("opUser", userID);
		resJson.put("remindInfo", addRemind.toJSONObject());
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
	public static NetMessageData obtainEventRemind(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");		
		List<EventRemindModel> erls = EventRemindDb.selectEventRemindByUserID(userID);
		if (erls != null && erls.size() > 0) {
			JSONArray jsonArray = new JSONArray();
			for (EventRemindModel er : erls) {
				jsonArray.add(er.toJSONObject());
			}
			resJson.put("remindArr", jsonArray);
		}
		
		resJson.put("resCode", 0);		
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
}
